<?php
/**
 * Definition class for table product.
 *
 * @package Toxter
 * @author Zodeken
 * @version $Id$
 */
class Toxter_Model_Product_DbTable extends Toxter_Model_Product_DbTable_Abstract
{
    protected $_name="product";
    protected $_primary="id";
    
    public function insertProduct($data) {
        
        $data['price'] = str_replace('.', '', $data['price']);
        $data['price'] = str_replace(' ', '', $data['price']);
        $data['price'] = str_replace('đ', '', $data['price']);
        
        $data['price_2'] = str_replace('.', '', $data['price_2']);
        $data['price_2'] = str_replace(' ', '', $data['price_2']);
        $data['price_2'] = str_replace('đ', '', $data['price_2']);
        
        return $this->insert($data);
    }
    
    public function getById($id) {
        return $this->fetchRow("id=$id");
    }
    
    public function getByUrl($url = null) {
       
        return $this->fetchRow("seo_url='$url'");
    }
    
    public function updateProduct($data, $id) {
        
        $data['price'] = str_replace('.', '', $data['price']);
        $data['price'] = str_replace(' ', '', $data['price']);
        $data['price'] = str_replace('đ', '', $data['price']);
        
        $data['price_2'] = str_replace('.', '', $data['price_2']);
        $data['price_2'] = str_replace(' ', '', $data['price_2']);
        $data['price_2'] = str_replace('đ', '', $data['price_2']);
        
        $this->update($data, "id = " .(int) $id);
    }
    
    public function searchProductByKw($kw = NULL){
        
        $this->Product = new Toxter_Model_Product_DbTable();
        $select = $this->_db->select()
                    ->from('product')
                    ->where("name LIKE ?", $kw.'%')
                    ->order('do_uu_tien DESC');
        return $this->getAdapter()->fetchAll($select);
    }
            
    
    public function deleteNumPlateInvalid () {
        return $this->_db->fetchAll('select np.id as nid, u.id as uid
            from number_plate np
            LEFT JOIN user u on u.id = np.user_id
            ');
    }
    
    public function searchProductForVehicle ($km, $dung_tich, $type) {
        // Search product
        $this->Product = new Toxter_Model_Product_DbTable();
       
        // 2T
        if ($type == 5) {
            $select = $this->_db->select()
                    ->from('product_category')
                    ->joinInner('product', "product.id = product_category.product_id 
                        AND product_category.cat_id = $type")
                    ->where("(dung_tich_1 >= $dung_tich AND dung_tich_1 <= 1) 
                                        OR (dung_tich_2 >= $dung_tich AND dung_tich_2 <= 1)")
                    ->order('do_uu_tien DESC');
            
        // 4T or TG   
        } else if ($type == 6 || $type == 4) {
            // Private condition
            $private_cond = '';
            
            // Km < 30000 or km , 3 nam
            if ($km == 1 || $km == 3) {
                // TG
                if ($type == 4) {
                    $private_cond = " OR chi_so_do_nhot_2 = 40";
                }
                $chi_so_do_nhot_2 = $type == 6 ? 40 : 30;
            } else {
                $chi_so_do_nhot_2 = $type == 6 ? 50 : 40; 
            }
            
            $select = $this->_db->select()
                    ->from('product_category')
                    ->joinInner('product', "product.id = product_category.product_id 
                        AND product_category.cat_id = $type")
                    ->where("((dung_tich_1 >= $dung_tich AND dung_tich_1 <= 1) 
                                        OR (dung_tich_2 >= $dung_tich AND dung_tich_2 <= 1)) 
                                        AND (chi_so_do_nhot_2 = $chi_so_do_nhot_2 $private_cond)")
                    ->order('do_uu_tien DESC');
            
        }
        //echo $select->__toString();exit;
        
        return $this->getAdapter()->fetchAll($select);
    }
    
    public function getByCatId ($cat_id) {
        $select = $this->_db->select()
                    ->from('product_category')
                    ->joinInner('product', "product.id = product_category.product_id AND product_category.cat_id = $cat_id")
                    ->order('name ASC');
       
        return $this->getAdapter()->fetchAll($select);
    }
    
    public function getByCatParentId($parent_id) {
        
        $select = $this->_db->select()
                    ->from('product_category')
                    ->joinInner('product', "product_category.product_id = product.id")
                    ->joinInner('category', "category.id = product_category.cat_id AND category.parent_id={$parent_id}")
                    ->order('product.do_uu_tien DESC');
           
        return $this->getAdapter()->fetchAll($select);
    }

    public function getAll() {
        return $this->fetchAll()->toArray();
    }
    
    public function getAllOrderBy( $field = 'name') {
        return $this->fetchAll($this->select()->order("$field ASC"))->toArray();
         
    }

        public function other_products($id = null) {
        
        $product = $this->getById($id);
        
        $c = new Toxter_Model_ProductCategory_DbTable();
        $pro_cat = $c->getByProductId($id);
        $join = "";
        if($pro_cat) {
            foreach ($pro_cat as $cat)
            {
                $join .= "product_category.cat_id = {$cat['cat_id']} OR "; 
            }
            $join .= " 1 = 0 ";
        }
       
        if($product) {
            $price_floor = $product->price - 10000;
            $price_over = $product->price + 10000;
            $select = $this->_db->select()
                    ->distinct()
                    ->from('product_category', 'product_id')
                    ->joinInner('product', "product.id = product_category.product_id AND ($join)")
                    ->where("product.id != $product->id AND chi_so_do_nhot_2 = $product->chi_so_do_nhot_2 AND price >= $price_floor AND price <= $price_over")
                    ->order('do_uu_tien DESC');
            
        
           //echo $select->__toString();exit;
        
            return $this->getAdapter()->fetchAll($select);
        }
        return false;
    }
    
    public function updateViews($id = null) {
            $product = $this->getById($id);
            $data['views'] = $product['views']+1;
            $this->update($data, "id = " .(int) $id);
    }
}
